// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    tapData: [
      { id: 0, destination: "全部"},
      { id: 1, destination: "滘口"},
      { id: 2, destination: "广州工商学院"},
      { id: 3, destination: "三水万达广场"},
      { id: 4, destination: "三水广场"}
    ],
    TabCur: 0,
    start: "",
    end: "",
    // 约车信息列表（包含所有情况）
    showList: [],

    // 全部
    array0: [],
    // 滘口
    array1: [],
    // 广州工商学院
    array2: [],
    // 三水万达广场
    array3: [],
    // 三水广场
    array4: [],

    // 模拟约车信息数据
    byCarInfo:[
      {
        start: "广州工商学院",
        end: "滘口",
        time:"2021-05-10 15:28",
        userPhone:"19925671849"
      },
      {
        start: "广州工商学院",
        end: "滘口",
        time:"2021-05-10 15:28",
        userPhone:"19925671849"
      },
      {
        start: "滘口",
        end: "广州工商学院",
        time:"2021-05-10 15:28",
        userPhone:"19925671849"
      },
      {
        start: "广州工商学院",
        end: "三水万达广场",
        time:"2021-05-10 15:28",
        userPhone:"19925671849"
      },
      {
        start: "广州工商学院",
        end: "三水广场",
        time:"2021-05-10 15:28",
        userPhone:"19925671849"
      }
    ]

  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad() {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }


    // 将请求存入对应目的地数组
    const tapData = this.data.tapData
    var array = this.data.array0
    for(var i=0 ;i < tapData.length;i++){

      var id = tapData[i].id
      console.log(id)

      if(id == 0){
        // 遍历约车数据
        var byCarInfo1 = this.data.byCarInfo
        byCarInfo1.forEach(item => {
          var listObject = {
            start: '',
            end: '',
            userPhone: '',
            time: ''
          }
          // 向该对象中添加属性
          listObject.start = item.start
          listObject.end = item.end
          listObject.userPhone = item.userPhone
          listObject.time = item.time
  
          console.log(listObject)

          array.push(listObject) 

        })
          // 向数组中添加对象
          var show = this.data.showList
          show.push(array)
          console.log(show)

          this.setData({
            showList: show,
            array0: array
          })

      }
      else if(id == 1){
        var listObject1 = {}
        var array1 = this.data.array1
        this.judgeEnd("滘口",listObject1,array1)
        this.setData({
          list: show,
          array1: array1
        })
      }
      else if(id == 2){
        var listObject2 = {}
        var array2 = this.data.array2
        this.judgeEnd("广州工商学院",listObject2,array2)
        this.setData({
          list: show,
          array2: array2
        })
      }
      else if(id == 3){
        var listObject3 = {}
        var array3 = this.data.array3
        this.judgeEnd("三水万达广场",listObject3,array3)
        this.setData({
          list: show,
          array3: array3
        })
      }
      else if(id == 4){
        var listObject4 = {}
        var array4 = this.data.array4
        this.judgeEnd("三水广场",listObject4,array4)
        this.setData({
          list: show,
          array4: array4
        })
      } 
    }
    // console.log("--------------------------------")
    // console.log(this.data.array0)
    // console.log(this.data.array1)
    // console.log(this.data.array2)
    // console.log(this.data.array3)
    // console.log(this.data.array4)
    console.log("--------------------------------")
    console.log(this.data.showList)
  },

  getUserInfo(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

  // 封装判断目的地方法
  judgeEnd(end,listObject1,array){

    // 遍历约车数据
    var byCarInfo1 = this.data.byCarInfo
    byCarInfo1.forEach(item => {
      listObject1 = {
        start: '',
        end: '',
        userPhone: '',
        time: ''
      }
      // 向该对象中添加属性
      if(item.end == end){
        listObject1.start = item.start
        listObject1.end = item.end
        listObject1.userPhone = item.userPhone
        listObject1.time = item.time
    
        console.log(listObject1)
        
        // 存入数组
        array.push(listObject1)
      }
      
    })
      // 向数组中添加对象
      var show = this.data.showList
      show.push(array)
      console.log(show)
       
  },

  // 切换导航条
  handleCurrent(e) {

    // console.log(e.currentTarget.dataset.id)

    if(e.currentTarget.dataset.id == 0){
      // console.log(show)
      this.setData({
        TabCur: e.currentTarget.dataset.id
      })
    }else if(e.currentTarget.dataset.id == 1){
      this.setData({
        TabCur: e.currentTarget.dataset.id,
      })
    }else if(e.currentTarget.dataset.id == 2){
      this.setData({
        TabCur: e.currentTarget.dataset.id,
      })
    }else if(e.currentTarget.dataset.id == 3){
      this.setData({
        TabCur: e.currentTarget.dataset.id,
      })
    }else if(e.currentTarget.dataset.id == 4){
      this.setData({
        TabCur: e.currentTarget.dataset.id,
      })
    }
  },

  // 接单
  receiving(e){

    console.log(e.currentTarget.dataset.id);
    if(app.globalData.orderList.length < app.globalData.seat){
    var orderObject = {
      start: '',
      end: '',
      userphone: '',
      time: ''
    }

    // 获取点击项的数据
    orderObject.start = this.data.showList[this.data.TabCur][e.currentTarget.dataset.id].start
    orderObject.end = this.data.showList[this.data.TabCur][e.currentTarget.dataset.id].end
    orderObject.userPhone = this.data.showList[this.data.TabCur][e.currentTarget.dataset.id].userPhone
    orderObject.time = this.data.showList[this.data.TabCur][e.currentTarget.dataset.id].time

    app.globalData.orderList.push(orderObject)
    console.log(app.globalData.orderList)
    }else{
      wx.showToast({
        title: "接单失败,已超出限载人数",
        icon: "none",
        duration: 2000
       })
    }

    // 发送请求,减去约车申请中该条申请,添加到订单列表中
  }
})


